package dse.messages.singleversion;

import dse.DataObject;
import dse.Experiment;
import dse.ExperimentRecorder;
import dse.Node;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessagePurpose;
import dse.messages.MessageSender;

public class RedirectShareDownloadMessageHandler
extends MessageSender
implements MessageHandler
{
	private DataObject dobj;
	private int shareIndex;
	private Node redirectTo;
	private MessagePurpose purpose;	
	private ShareDownloader shareDownloader;
	
	
	
	public RedirectShareDownloadMessageHandler (
			Experiment experiment, 
			DataObject dobj, 
			int shareIndex,
			Node redirectTo,
			MessagePurpose purpose,
			ShareDownloader shareDownloader
			)
	{
		super(experiment);
		
		this.dobj = dobj;
		this.shareIndex = shareIndex;
		this.redirectTo = redirectTo;
		this.purpose = purpose;
		this.shareDownloader = shareDownloader;
	}
	
	
	
	
	public boolean acceptMessage (
			Message message
			)
	{
		return true;
	}
	
	public void messageReceived (
			ExperimentRecorder recorder, 
			Message message,
			double timeLeft
			)
	{
		recorder.debug(this, "msgRcvd: " + message, timeLeft);
		
		// Now the origional requester (message.to()) has to send a get share
		// messag to the node we are redirecting to!		
		Message m = super.createRequestShareForeignMessage(
				message.to(), this.redirectTo, dobj, shareIndex,
				purpose, shareDownloader
				);
		recorder.debug(this, "new msg: " + m, timeLeft);
		//experiment.messageQueue().enqueueMessageInPeriod(m, timeLeft);
	}
	
	public void messageInterupted (
			ExperimentRecorder recorder,
			Message message, 
			double timeLeft
			)
	{
		recorder.debug(this, "msgIntrpt: " + message, timeLeft);
		
		shareDownloader.shareDownloadFailed(
				recorder, dobj, shareIndex, timeLeft
				);
	}



}
